package com.offcn.servlet;

import com.alibaba.fastjson.JSON;
import com.offcn.utils.PageTool;
import com.offcn.entity.ResultBean;
import com.offcn.entity.User;
import com.offcn.service.Userservice;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/user")
public class UserServlet extends BaseServlet {

    private Userservice userService = new Userservice();
    private ResultBean resultBean = null;
    //进行用户名密码判断
    private void adminLongin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String imageCode = request.getParameter("imageCode");
        //获取后台中的验证码
        HttpSession session = request.getSession();
        String verCode = (String) session.getAttribute("verCode");
        if (true
//                imageCode.equalsIgnoreCase(verCode)
        ) {
            boolean adminLongin = userService.adminLongin(new User(username, password), session);
            //方便登录 使用true解除验证码控制
            if (true) {
                resultBean = new ResultBean(200, "登陆成功", adminLongin);
            } else {
                resultBean = new ResultBean(500, "用户名或密码输入错误，请重新输入", adminLongin);
            }
        } else {
            resultBean = new ResultBean(500, "验证码输入错误,请重新输入", null);
        }
        String s = JSON.toJSONString(resultBean);
        response.getWriter().write(s);
    }
    //判断是否登录
    private void findUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        if (user != null) {
            resultBean = new ResultBean(200, "登录成功", user);
        } else {
            resultBean = new ResultBean(500, "小伙子，你登陆了么？！！！", null);
        }
        String s = JSON.toJSONString(resultBean);
        response.getWriter().write(s);
    }

    //退出登录的方法
    private void logout(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        session.invalidate();
    }

    //展示所有用户的方法
    private void findAllUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String search = request.getParameter("search");
        String currentPage = request.getParameter("currentpage");
        String pageSize = request.getParameter("pageSize");
        System.out.println(pageSize);
        System.out.println("currentPage=--" + currentPage);
        int totalCount = userService.findAllCount(search);
        PageTool pageTool = new PageTool(totalCount, currentPage, Integer.parseInt(pageSize));
        List<User> users = userService.findAllUser(pageTool, search);
        pageTool.setList(users);
        if (users != null) {
            resultBean = new ResultBean(200, "获取用户成功", pageTool);
        } else {
            resultBean = new ResultBean(500, "获取用户失败", pageTool);
        }
        String s = JSON.toJSONString(resultBean);
        response.getWriter().write(s);
    }
    //添加用户的方法
    private void addUser(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException, IOException {
        Map<String, String[]> parameterMap = request.getParameterMap();
        User user = new User();
        BeanUtils.populate(user, parameterMap);
        boolean users = userService.addUser(user);
        if (users) {
            resultBean = new ResultBean(200,"添加成功",users);
        }else {
            resultBean = new ResultBean(500,"添加失败",users);

        }
        String s = JSON.toJSONString(resultBean);
        response.getWriter().write(s);
    }
    //删除用户
    private  void delectUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String uids = request.getParameter("uids");
        if (uids!=null){

        boolean ids= userService.delectUser(uids);

        if (ids) {
            resultBean = new ResultBean(200,"删除成功",ids);
        }else {
            resultBean = new ResultBean(500,"删除失败",ids);

        }
        }
        String s = JSON.toJSONString(resultBean);
        response.getWriter().write(s);
    }
    //用户信息修改
    private  void updateUser(HttpServletRequest request, HttpServletResponse response) throws IOException, InvocationTargetException, IllegalAccessException {
        Map<String, String[]> parameterMap = request.getParameterMap();
        User user = new User();
        BeanUtils.populate(user,parameterMap);
        System.out.println(user);
        if (user != null){

        boolean ids= userService.updateUser(user);

        if (ids) {
            resultBean = new ResultBean(200,"修改成功",ids);
        }else {
            resultBean = new ResultBean(500,"修改失败",ids);

        }
        }
        String s = JSON.toJSONString(resultBean);
        response.getWriter().write(s);
    }



}
